﻿<%@ Page Language="C#" MasterPageFile="~/MasterPages/WideContent.Master" AutoEventWireup="true" CodeBehind="DailyTimeRecords.aspx.cs" Inherits="Legazpi.Payroll.Web.Admin.Payroll.DailyTimeRecords" %>
<%@ Import Namespace="Legazpi.Payroll.Web.Models" %>
<%@ Import Namespace="Legazpi.Payroll.DataAccess.DataObjects.Enums" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script src="<%=ResolveUrl("~/Scripts/jquery.timepicker.min.js")%>" type="text/javascript"></script>
    <link href="<%=ResolveUrl("~/Styles/jquery.timepicker.css")%>" rel="stylesheet" type="text/css" />
    <title></title>
		<script type="text/javascript">
            var startDay = <%=StartDay %>;
            var endDay = <%=EndDay %>;
            var curPayrollId = <%=CurrentPayrollDateId %>;
            
            var serviceUrl = '<%=ResolveUrl("~/Webservices/PayrollWebService.asmx")%>';

            $(document).ready(function () {
                $('.List_Button.Save').click(function (e) {
                    e.preventDefault();
                    //SaveBtn_EmployeeID

                    var arrElement = $(this).attr("id").split('_');
                    var employeeId = arrElement[1];
                    var dtrId = $('#<%=DTRKey %>' + '_' + employeeId).attr("id").split('_')[1];
                    var saveButton = $(this);
                    var ajaxLoader = $('#ajaxLoader_' + employeeId);
                    saveButton.hide();
                    ajaxLoader.show();                    

                    var _timeRecords = [];
                    for (var day = startDay; day <= endDay; ++day) {  
                        //IdentifierName_DayID_EmployeeID   
                        var holidayElement = '#<%=DDHoliday %>' + '_' + day + '_' + employeeId; 
                        var timeInElement = '#<%=TimeInID %>' + '_' + day + '_' + employeeId;
                        var timeOutElement = '#<%=TimeOutID %>' + '_' + day + '_' + employeeId;
                        
		                var holiday = $(holidayElement).val();
                        var timeIn = $(timeInElement).timepicker('getTime');
		                var timeOut = $(timeOutElement).timepicker('getTime');

                        if(timeIn > timeOut){
                            timeOut.setDate(timeIn.getDate()+1);
                        }
                        _timeRecords.push({ Day: day, TimeIn: timeIn, TimeOut: timeOut, Holiday: holiday });
                    }
                    var _dtrItem = {
                        TimeRecords : _timeRecords,
                        EmployeeId  : employeeId,
                        PayrollDateId : curPayrollId,
                        DayTimeRecordId : dtrId
                    };
                    var json = JSON.stringify({ dtrItem: _dtrItem });
                    
                    $.ajax({
                        type: "POST",
                        url: serviceUrl + "/SaveEmployeeDTR",
                        data: json,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var response = eval('(' + data.d + ')');
                            if(response.Success)
                            {
                                alert('Saved!');                                
                                $('#WH_' + employeeId).val(response.WorkHours);                                
                                $('#OT_' + employeeId).val(response.OverTime);
                                $('#ND_' + employeeId).val(response.NightDiff);
                                ComputeTotalHours();
                            }                            
                        },
                        complete: function () {
                            saveButton.show();   
                            ajaxLoader.hide();     
                        }

                    });

                });
                //Init Totals
                ComputeTotalHours();
            });
            
            var arrEmpId = [<%=ArrayEmployeeId %>];
		    $(function () {
		        $('.inputTime').timepicker({
		            //'timeFormat': 'H:i'
		        });

		        $('.inputTime').on('changeTime', function () {
		            //IdentifierName_DayID_EmployeeID
		            var arrElement = $(this).attr("id").split('_');
		            var dayId = arrElement[1];
                    var employeeId = arrElement[2];

		            ComputeHoursPerDay(dayId, employeeId);
                    ComputeTotalWorkHours(employeeId);
		        });  

		        $('.selectAll').change( function () {
		            //IdentifierName_DayID		
                    
                    var identifierName = $(this).attr("id");   
                    var selValue = $(this).val();                                                       
                    for(var i = 0; i <= arrEmpId.length -1 ; i++)
                    {
                        var selectElementId = '#' + identifierName + '_' + arrEmpId[i].toString();
                        $(selectElementId).val(selValue);                        
                    }
		        });  
                              
		    });

		    function ComputeHoursPerDay(dayId,employeeId) {                
		        var timeInElement = '#<%=TimeInID %>' + '_' + dayId + '_' + employeeId;
		        var timeOutElement = '#<%=TimeOutID %>' + '_' + dayId + '_' + employeeId;
		        var totDayHourElement = '#<%=TotDayHourID %>' + '_' + dayId + '_' + employeeId;                

		        var timeIn = $(timeInElement).timepicker('getTime');
		        var timeOut = $(timeOutElement).timepicker('getTime');

                if(timeIn > timeOut){
                    timeOut.setDate(timeIn.getDate()+1);
                }

		        var diff = timeOut - timeIn;
		        var totHour = diff / 1000 / 60 / 60;

                //set total hour on a day
                if(totHour < 0) {totHour = 0};
                if($(timeInElement).val() == '' || $(timeOutElement).val() == '' ) {totHour = 0};

		        $(totDayHourElement).val(totHour);
		    }
		    
            function ComputeTotalWorkHours(employeeId) { 
                var totalHours = 0;
                var regWorkHoursElement = '#<%=RegWorkHours %>' + '_' + employeeId;
                for (var day = startDay; day <= endDay; ++day) {                    
                    var totDayHourElement = '#<%=TotDayHourID %>' + '_' + day + '_' + employeeId;
                    var hrs = $(totDayHourElement).val();
                    if(IsNumeric(hrs))
                    {
                        totalHours += parseFloat(hrs);
                    }
                }
                //set total hour per employee                
                $(regWorkHoursElement).val(totalHours);
                
            }
            function IsNumeric(n) {
              return !isNaN(parseFloat(n)) && isFinite(n);
            }

            function ComputeTotalHours(){
                var regTotal = 0;                
                var otTotal = 0;
                var ndTotal = 0;
                var totalHours = 0;

                for(var i = 0; i <= arrEmpId.length -1 ; i++)
                {
                    var regHoursTotal = '#WH_' + arrEmpId[i].toString();                    
                    var otHoursTotal = '#OT_' + arrEmpId[i].toString();
                    var ndHoursTotal = '#ND_' + arrEmpId[i].toString();
                    
                    regTotal += IsNumeric($(regHoursTotal).val()) ? parseFloat($(regHoursTotal).val()) : 0.0 ;                    
                    otTotal += IsNumeric($(otHoursTotal).val()) ? parseFloat($(otHoursTotal).val()) : 0.0 ;                    
                    ndTotal += IsNumeric($(ndHoursTotal).val()) ? parseFloat($(ndHoursTotal).val()) : 0.0 ;                    
                }      
                totalHours = regTotal +  otTotal +  ndTotal;          
                $('#regHoursTotal').html(regTotal);
                $('#otHoursTotal').html(otTotal);
                $('#ndHoursTotal').html(ndTotal);
                $('#totalHours').html(totalHours);
            }

		</script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">    
        <center><h1>Payroll Period Covered:</h1>  <strong><asp:Literal ID="literalPayrollDateCovered" runat="server"></asp:Literal></strong> </center>
        <br />
        <hr />
        <p>
            <label for="DeployedDropdown">Deployed At:</label>                    
            <asp:DropDownList ID="DeployedDropdown" runat="server" AppendDataBoundItems="true" AutoPostBack="true">
                <asp:ListItem Text="--Select--" Value="0" /> 
            </asp:DropDownList>                    
                    
        </p>
        <asp:Repeater ID="dtrRepeater" runat="server" 
            onitemdatabound="dtrRepeater_ItemDataBound">
            <HeaderTemplate>
                <table width="100%" class="tblDTR">
                    <tbody>
                        <tr>
                            <th rowspan="3" style="width:100px">Name</th>
                            <th rowspan="3">In/Out</th>
                            <th colspan="<%=ColumnDaySpan %>">Days</th>
                            <th rowspan="3">Total</th>                            
                        </tr>                        
                        <tr>                            
                            <%for (int iDay = StartDay; iDay <= EndDay; iDay++)
                              {%>                              
                                <th width="40px" align="center"><%= iDay.ToString().PadLeft(2, '0')%></th>
                            <%} %>
                        </tr>
                        <tr>
                            <%for (int iDay = StartDay; iDay <= EndDay; iDay++)
                              {
                                  string strInputID = string.Format("{0}_{1}", DDHoliday, iDay);%>
                                <td>
                                    <select class="selectAll" id="<%=strInputID %>">
   								        <option value="<%=(int)HolidayType.None %>">- - -</option>
   								        <option value="<%=(int)HolidayType.Legal %>">LGL</option>
   								        <option value="<%=(int)HolidayType.Special %>">SPC</option>
  							        </select>
                                </td>
                            <%} %>
                        </tr>
            </HeaderTemplate>
            <ItemTemplate>                
                <tr>                    
                    <td rowspan="4" class="name">
                        <a href="EmployeePayrollSummary.aspx?empid=<%# Eval("EmployeeId")%>">
                        <%# Eval("EmployeeName")%>
                        </a>
                        <input type="hidden" id="<%=DTRKey %>_<%# Eval("EmployeeId")%>" value="<%# Eval("Id")%>" />
                    </td>                                                                     
                    <td align="right">In</td>
                    <asp:Literal ID="ColumnDataTimeIn" runat="server"></asp:Literal>
                    <td rowspan="4" class="totals">
                        WH:<input type="text" class="inputTotHrs" id="WH_<%# Eval("EmployeeId")%>" value="<%# Eval("WorkHours")%>" /><br />
                        OT:<input type="text" class="inputTotHrs" id="OT_<%# Eval("EmployeeId")%>" value="<%# Eval("OverTime")%>" /><br />
                        ND:<input type="text" class="inputTotHrs" id="ND_<%# Eval("EmployeeId")%>" value="<%# Eval("NightDiff")%>" /><br />
                        <br />                            
                        <input type="image" id="<%=SaveBtn %>_<%# Eval("EmployeeId")%>" value="" class="List_Button Save" />     
                        <img src="../../images/ajax_loader_small.gif" id="ajaxLoader_<%# Eval("EmployeeId")%>" style="display:none" />                   
                    </td>
                </tr>
                <tr>
                    <td align="right">Out</td>
                    <asp:Literal ID="ColumnDataTimeOut" runat="server"></asp:Literal>
                </tr>
                <tr>
                    <td align="right">Hours</td>
                    <asp:Literal ID="ColumnDataHours" runat="server"></asp:Literal>
                </tr>
                <tr>
                    <td align="right">Holiday</td>
                    <asp:Literal ID="ColumnSelectedHoliday" runat="server"></asp:Literal>
                </tr>
            </ItemTemplate>
            <AlternatingItemTemplate>                         
                <tr class="alt">
                    <td rowspan="4" class="nameAlt">
                        <a href="EmployeePayrollSummary.aspx?empid=<%# Eval("EmployeeId")%>">
                        <%# Eval("EmployeeName")%>
                        </a>
                        <input type="hidden" id="<%=DTRKey %>_<%# Eval("EmployeeId")%>" value="<%# Eval("Id")%>" />
                    </td>
                    <td align="right">In</td>
                    <asp:Literal ID="ColumnDataTimeIn" runat="server"></asp:Literal>

                    <td rowspan="4" class="totals">
                        WH:<input type="text" class="inputTotHrs" id="WH_<%# Eval("EmployeeId")%>" value="<%# Eval("WorkHours")%>" /><br />
                        OT:<input type="text" class="inputTotHrs" id="OT_<%# Eval("EmployeeId")%>" value="<%# Eval("OverTime")%>" /><br />
                        ND:<input type="text" class="inputTotHrs" id="ND_<%# Eval("EmployeeId")%>" value="<%# Eval("NightDiff")%>" /><br />
                        <br />                            
                        <input type="image" id="<%=SaveBtn %>_<%# Eval("EmployeeId")%>" value="" class="List_Button Save" />                        
                    </td>
                </tr>
                <tr class="alt">
                    <td>Out</td>
                    <asp:Literal ID="ColumnDataTimeOut" runat="server"></asp:Literal>
                </tr>
                <tr class="alt">
                    <td>Hours</td>
                    <asp:Literal ID="ColumnDataHours" runat="server"></asp:Literal>
                </tr>
                <tr class="alt">
                    <td>Holiday</td>
                    <asp:Literal ID="ColumnSelectedHoliday" runat="server"></asp:Literal>
                </tr>            
            </AlternatingItemTemplate>
            <FooterTemplate>
                        <tr>
                            <td colspan="<%=FooterColumnSpan %>" class="totalno">Regular Hours:</td>
                            <td class="totalamt" id="regHoursTotal"></td>
                        </tr>
                        <tr>
                            <td colspan="<%=FooterColumnSpan %>" class="totalno">Overtime Hours:</td>
                            <td class="totalamt" id="otHoursTotal"></td>
                        </tr>
                        <tr>
                            <td colspan="<%=FooterColumnSpan %>" class="totalno">Night Diff Hours:</td>
                            <td class="totalamt" id="ndHoursTotal"></td>
                        </tr>
                        <tr>
                            <td colspan="<%=FooterColumnSpan %>" class="totalno">Total No of Hours:</td>
                            <td class="totalamt" id="totalHours"></td>
                        </tr>
                    </tbody>
                </table>
            </FooterTemplate>
        </asp:Repeater>    
</asp:Content>
